/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package textCleaning;

/**
 *
 * @author hubert
 */
public class textCleaner {
    private java.lang.StringBuilder modText;
    
    /**
     * Removes all digits from the input StringBuilder text and returns it
     * @param in
     * @return in but without digits
     */
    public static java.lang.StringBuilder removeNumerals(java.lang.StringBuilder in){
        for(int i = 0; i < in.length(); i++){
            char c = in.charAt(i);
            if(Character.isDigit(c)){
                in.deleteCharAt(i);
                i = i-1;
            }
        }
        return in;
    }
    
    /**
     * Coverts the input text to all lowercase
     * @param in
     * @return in but all lowercase
     */
    public static java.lang.StringBuilder toLowerCase(java.lang.StringBuilder in){
        for(int i = 0; i < in.length(); i++){
            char c = in.charAt(i);
            if(Character.isUpperCase(c)){
                in.replace(i, i+1, Character.toLowerCase(c)+"");
            }
        }
        return in;
    }
    
    public static java.lang.StringBuilder handleLinkText(java.lang.StringBuilder bodyText, java.util.ArrayList<dataObjects.hyperLink> links){
        String prefix = " href";
        int strt = 0;
        for(dataObjects.hyperLink lnk : links){
            int strtPos = bodyText.indexOf(lnk.getLinkText(), strt);
            try{
                bodyText.replace(strtPos, (strtPos + lnk.getLinkText().length()), (prefix + lnk.getLinkText().replaceAll(" ", "")));
            }catch(java.lang.StringIndexOutOfBoundsException e){
                System.out.println(lnk.getLinkText() + " : " + lnk.getTargetURL());
            }
            
            strt = strtPos;
        }
        return bodyText;
    }
    
    
    /**
     * Removes all punctuation from input text and then returns it
     * @param in
     * @return in w/o punctuation
     */
    public static java.lang.StringBuilder removePunctuation(java.lang.StringBuilder in){
        for(int i = 0; i < in.length(); i++){
            char c = in.charAt(i);
            
            if(isPunctuation(c)){
                in.deleteCharAt(i);
                i = i-1;
            }
        }
        return in;
    }
    
    /**
     * Determines if the char in question is "punctuation"
     * @param c
     * @return true if char is punctuation, false otherwise
     */
    private static boolean isPunctuation(char c){
        boolean serve = false;
        int asciiC = (int)c;
        if(asciiC >= 33 && asciiC <= 47){
            serve = true;
        }
        if(asciiC >= 58 && asciiC <= 64){
            serve = true;
        }
        if(asciiC >= 91 && asciiC <= 96){
            serve = true;
        }
        if(asciiC >= 123 && asciiC <= 125){
            serve = true;
        }
        
        return serve;
    }
    
    /**
     * Runs all the subtractive text processing methods in this program
     * @param in
     * @return in after being processed in series by all subtractive text processing methods in this program
     */
    public static java.lang.StringBuilder runAllRemovalFuncs(java.lang.StringBuilder in){
        return removePunctuation(removeNumerals(toLowerCase(in)));
    }
    
    /**
     * Test Function
     * @param args 
     */
    public static void main(String[] args){
        //System.out.println(toLowerCase(new java.lang.StringBuilder("A")));
        dataObjects.hyperLink a = new dataObjects.hyperLink();
        a.setLinkText("good Morning");
        java.util.ArrayList<dataObjects.hyperLink> as = new java.util.ArrayList<dataObjects.hyperLink>();
        as.add(a);
        java.lang.StringBuilder bodyText = new java.lang.StringBuilder("good Morning hello, good Morning rise and shine. good Morning");
        System.out.println(handleLinkText(bodyText, as).toString());
    }
}
